<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>fakeworkerpool.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
  </script>

  <script src="static/js/doc.js">
  </script>

  <meta charset="utf8">
</head>

<body onload="prettyPrint()">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_gears_fakeworkerpool.js.html">fakeworkerpool.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line2"></a>// you may not use this file except in compliance with the License.
<a name="line3"></a>// You may obtain a copy of the License at
<a name="line4"></a>//
<a name="line5"></a>//     http://www.apache.org/licenses/LICENSE-2.0
<a name="line6"></a>//
<a name="line7"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line8"></a>// distributed under the License is distributed on an &quot;AS IS&quot; BASIS,
<a name="line9"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line10"></a>// See the License for the specific language governing permissions and
<a name="line11"></a>// limitations under the License.
<a name="line12"></a>
<a name="line13"></a>// Copyright 2007 Google Inc. All Rights Reserved.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview This implementation of goog.gears.WorkerPool uses
<a name="line17"></a> * a fake worker pool (FakeWorkerPool_) that is implemented as an iframe in the
<a name="line18"></a> * current document.
<a name="line19"></a> */
<a name="line20"></a>
<a name="line21"></a>goog.provide(&#39;goog.gears.FakeWorkerPool&#39;);
<a name="line22"></a>
<a name="line23"></a>goog.require(&#39;goog.Uri&#39;);
<a name="line24"></a>goog.require(&#39;goog.gears&#39;);
<a name="line25"></a>goog.require(&#39;goog.gears.WorkerPool&#39;);
<a name="line26"></a>goog.require(&#39;goog.net.XmlHttp&#39;);
<a name="line27"></a>
<a name="line28"></a>
<a name="line29"></a>/**
<a name="line30"></a> * This class implements a fake worker pool. It has the same interface as
<a name="line31"></a> * the Gears WorkerPool object but uses an iframe and timers to emulate the
<a name="line32"></a> * async nature of the message passing.
<a name="line33"></a> * @constructor
<a name="line34"></a> * @private
<a name="line35"></a> */
<a name="line36"></a>goog.gears.FakeWorkerPool_ = function() {
<a name="line37"></a>  /**
<a name="line38"></a>   * Map from worker id to the frame element name
<a name="line39"></a>   * @private
<a name="line40"></a>   */
<a name="line41"></a>  this.frameNames_ = {};
<a name="line42"></a>};
<a name="line43"></a>
<a name="line44"></a>
<a name="line45"></a>/**
<a name="line46"></a> * Counter used to ensure that the frame get unique names
<a name="line47"></a> * @type {number}
<a name="line48"></a> * @private
<a name="line49"></a> */
<a name="line50"></a>goog.gears.FakeWorkerPool_.idCounter_ = 1;
<a name="line51"></a>
<a name="line52"></a>
<a name="line53"></a>/**
<a name="line54"></a> * The id of the main worker
<a name="line55"></a> * @type {number}
<a name="line56"></a> * @private
<a name="line57"></a> */
<a name="line58"></a>goog.gears.FakeWorkerPool_.mainWorkerId_ = 0;
<a name="line59"></a>
<a name="line60"></a>
<a name="line61"></a>/**
<a name="line62"></a> * Creates a new worker.
<a name="line63"></a> * @param {string} url  URL from which to get the code to execute inside the
<a name="line64"></a> *     worker.
<a name="line65"></a> * @return {number} The ID of the worker that was created.
<a name="line66"></a> */
<a name="line67"></a>goog.gears.FakeWorkerPool_.prototype.createWorkerFromUrl = function(url) {
<a name="line68"></a>  // TODO make this async
<a name="line69"></a>  var xhr = new goog.net.XmlHttp();
<a name="line70"></a>  xhr.open(&#39;GET&#39;, url, false);
<a name="line71"></a>  xhr.send(null);
<a name="line72"></a>  return this.createWorker(xhr.responseText);
<a name="line73"></a>};
<a name="line74"></a>
<a name="line75"></a>
<a name="line76"></a>/**
<a name="line77"></a> * Creates a worker and evals the code inside the worker.
<a name="line78"></a> * @param {string} code  The JavaScript code to evaluate in the worker.
<a name="line79"></a> * @return {number} The ID of the worker that was created.
<a name="line80"></a> */
<a name="line81"></a>goog.gears.FakeWorkerPool_.prototype.createWorker =
<a name="line82"></a>    function(code) {
<a name="line83"></a>  // HACK: Since this code is included in a worker thread we cannot
<a name="line84"></a>  // directly reference window
<a name="line85"></a>  var win = goog.getObjectByName(&#39;window&#39;);
<a name="line86"></a>  // This will be dead code on a worker thread so we don&#39;t get here. It is
<a name="line87"></a>  // therefore OK to access win.document without square brackets.
<a name="line88"></a>  var doc = win.document;
<a name="line89"></a>  var iframeElement = doc.createElement(&#39;iframe&#39;);
<a name="line90"></a>  var id = goog.gears.FakeWorkerPool_.idCounter_++;
<a name="line91"></a>  var name = iframeElement.name = iframeElement.id = &#39;fake-worker-&#39; + id;
<a name="line92"></a>  doc.body.appendChild(iframeElement);
<a name="line93"></a>  var w = win.frames[name];
<a name="line94"></a>
<a name="line95"></a>  this.frameNames_[id] = name;
<a name="line96"></a>  var selfObj = this;
<a name="line97"></a>  var fakeGearsWorkerPool = {
<a name="line98"></a>    &#39;sendMessage&#39;: function(message, toId) {
<a name="line99"></a>      w.setTimeout(function() {
<a name="line100"></a>        selfObj.routeMessage_(message, id, toId);
<a name="line101"></a>      }, 1);
<a name="line102"></a>    },
<a name="line103"></a>
<a name="line104"></a>    &#39;allowCrossOrigin&#39;: function() {
<a name="line105"></a>      // Do nothing.
<a name="line106"></a>    }
<a name="line107"></a>  };
<a name="line108"></a>
<a name="line109"></a>  doc = w.document;
<a name="line110"></a>  doc.open();
<a name="line111"></a>  w[&#39;google&#39;] = {
<a name="line112"></a>    &#39;gears&#39;: {
<a name="line113"></a>      &#39;workerPool&#39;: fakeGearsWorkerPool,
<a name="line114"></a>      &#39;factory&#39;: goog.gears.getFactory()
<a name="line115"></a>    }
<a name="line116"></a>  };
<a name="line117"></a>
<a name="line118"></a>  // Make window, document and navigator undefined in the worker scope.
<a name="line119"></a>  // We cannot just set these to undefined because the properties are read
<a name="line120"></a>  // only. We therefore use a with statement to hide them to the scope.
<a name="line121"></a>  doc.write(&#39;&lt;script&gt;with ({window: undefined, document: undefined, &#39; +
<a name="line122"></a>            &#39;navigator: undefined}) {&#39; + code + &#39;}&lt;/script&gt;&#39;);
<a name="line123"></a>  doc.close();
<a name="line124"></a>
<a name="line125"></a>  return id;
<a name="line126"></a>};
<a name="line127"></a>
<a name="line128"></a>
<a name="line129"></a>/**
<a name="line130"></a> * Allows the worker who calls this to be used cross origin. This is not
<a name="line131"></a> * currently supported.
<a name="line132"></a> */
<a name="line133"></a>goog.gears.FakeWorkerPool_.prototype.allowCrossOrigin = function() {
<a name="line134"></a>  // Do nothing. This is not currently supported.
<a name="line135"></a>};
<a name="line136"></a>
<a name="line137"></a>
<a name="line138"></a>/**
<a name="line139"></a> * Sends a message to a worker
<a name="line140"></a> * @param {string} message  The message to send.
<a name="line141"></a> * @param {number} workerId  The id of the worker to send the message to.
<a name="line142"></a> */
<a name="line143"></a>goog.gears.FakeWorkerPool_.prototype.sendMessage =
<a name="line144"></a>    function(message, workerId) {
<a name="line145"></a>  var w = this.getWindow_(workerId);
<a name="line146"></a>  var messageObject = this.createMessageObject_(message, workerId);
<a name="line147"></a>  // Use w.setTimeout instead of window.setTimeout because JSUnit overides that
<a name="line148"></a>  w.setTimeout(function() {
<a name="line149"></a>    w[&#39;google&#39;][&#39;gears&#39;][&#39;workerPool&#39;].onmessage(
<a name="line150"></a>        String(message), goog.gears.FakeWorkerPool_.mainWorkerId_,
<a name="line151"></a>        messageObject);
<a name="line152"></a>  }, 1);
<a name="line153"></a>};
<a name="line154"></a>
<a name="line155"></a>
<a name="line156"></a>/**
<a name="line157"></a> * Callback for messages sent to this worker. Override to handle incoming
<a name="line158"></a> * messages
<a name="line159"></a> * @param {string} message  The message sent to this worker.
<a name="line160"></a> * @param {number} sender  The id of the worker that sent this message.
<a name="line161"></a> * @param {Object} opt_messageObject An object containing all information about
<a name="line162"></a> *     the message.
<a name="line163"></a> */
<a name="line164"></a>goog.gears.FakeWorkerPool_.prototype.onmessage = function(message,
<a name="line165"></a>                                                          sender,
<a name="line166"></a>                                                          opt_messageObject) {
<a name="line167"></a>  // Intentionally empty. The user should override this on the instance.
<a name="line168"></a>};
<a name="line169"></a>
<a name="line170"></a>
<a name="line171"></a>/**
<a name="line172"></a> * Routes the message to the right frame
<a name="line173"></a> * @param {string} message  The message sent to this worker.
<a name="line174"></a> * @param {number} fromId  The id of the worker that sent this message.
<a name="line175"></a> * @param {number} toId  The id of the worker to send the message to.
<a name="line176"></a> * @private
<a name="line177"></a> */
<a name="line178"></a>goog.gears.FakeWorkerPool_.prototype.routeMessage_ =
<a name="line179"></a>    function(message, fromId, toId) {
<a name="line180"></a>
<a name="line181"></a>  var messageObject = this.createMessageObject_(message, fromId);
<a name="line182"></a>  if (toId == goog.gears.FakeWorkerPool_.mainWorkerId_) {
<a name="line183"></a>    this.onmessage(message, fromId, messageObject);
<a name="line184"></a>  } else {
<a name="line185"></a>    var w = this.getWindow_(toId);
<a name="line186"></a>    w[&#39;google&#39;][&#39;gears&#39;][&#39;workerPool&#39;].onmessage(
<a name="line187"></a>        message, fromId, messageObject);
<a name="line188"></a>  }
<a name="line189"></a>};
<a name="line190"></a>
<a name="line191"></a>
<a name="line192"></a>/**
<a name="line193"></a> * Creates the message object.
<a name="line194"></a> * @param {*} text The text of the message.
<a name="line195"></a> * @param {number} sender The id of the sender.
<a name="line196"></a> * @return {Object} The message object.
<a name="line197"></a> * @private
<a name="line198"></a> */
<a name="line199"></a>goog.gears.FakeWorkerPool_.prototype.createMessageObject_ = function(
<a name="line200"></a>    text, sender) {
<a name="line201"></a>  // Use the current window&#39;s location since this class currently does not
<a name="line202"></a>  // allow cross-domain creation of workers.
<a name="line203"></a>  var uri = new goog.Uri(goog.getObjectByName(&#39;window&#39;).location);
<a name="line204"></a>  // The origin is represented as SCHEME://DOMAIN[:PORT]. Standard ports 80
<a name="line205"></a>  // and 443 are omitted.
<a name="line206"></a>  var port = uri.getPort();
<a name="line207"></a>  var origin = uri.getScheme() + &#39;://&#39; + uri.getDomain() +
<a name="line208"></a>      (uri.hasPort() &amp;&amp; port != 80 &amp;&amp; port != 443 ? &#39;:&#39; + uri.getPort() : &#39;&#39;);
<a name="line209"></a>  return {&#39;text&#39;: text, &#39;sender&#39;: sender, &#39;origin&#39;: origin, &#39;body&#39;: text};
<a name="line210"></a>};
<a name="line211"></a>
<a name="line212"></a>
<a name="line213"></a>/**
<a name="line214"></a> * Returns the window/frame that is used to emulate the thread
<a name="line215"></a> * @param {number} workerId The id of the worker.
<a name="line216"></a> * @return {Window} The window/frame that is used to emulate the thread.
<a name="line217"></a> * @private
<a name="line218"></a> */
<a name="line219"></a>goog.gears.FakeWorkerPool_.prototype.getWindow_ = function(workerId) {
<a name="line220"></a>  var frameName = this.frameNames_[workerId];
<a name="line221"></a>  if (frameName) {
<a name="line222"></a>    // HACK: Since this code is included in a worker thread we cannot
<a name="line223"></a>    // directly reference window
<a name="line224"></a>    var w = goog.getObjectByName(&#39;window.frames&#39;)[frameName];
<a name="line225"></a>    if (w) return w;
<a name="line226"></a>  }
<a name="line227"></a>  throw Error(&#39;Could not access worker&#39;);
<a name="line228"></a>};
<a name="line229"></a>
<a name="line230"></a>
<a name="line231"></a>
<a name="line232"></a>
<a name="line233"></a>/**
<a name="line234"></a> * The fake goog.gears.WorkerPool. Instead of wrapping the real Gears
<a name="line235"></a> * WorkerPool it uses goog.gears.FakeWorkerPool_
<a name="line236"></a> *
<a name="line237"></a> * @constructor
<a name="line238"></a> * @extends {goog.gears.WorkerPool}
<a name="line239"></a> */
<a name="line240"></a>goog.gears.FakeWorkerPool = function() {
<a name="line241"></a>  goog.gears.WorkerPool.call(this);
<a name="line242"></a>};
<a name="line243"></a>goog.inherits(goog.gears.FakeWorkerPool, goog.gears.WorkerPool);
<a name="line244"></a>
<a name="line245"></a>
<a name="line246"></a>/**
<a name="line247"></a> * @return {Object} A fake Gears WorkerPool object.
<a name="line248"></a> * @protected
<a name="line249"></a> */
<a name="line250"></a>goog.gears.FakeWorkerPool.prototype.getGearsWorkerPool = function() {
<a name="line251"></a>  return new goog.gears.FakeWorkerPool_;
<a name="line252"></a>};
</pre>


</body>
</html>
